#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("计算器");

    QIcon con("D:\\myQt\\Qtphoto\\700D488DF7A7C10DD51C5AEF324A51BB.png");
    ui->pushButton_huitui->setIcon(con);

    ui->pushButton_equal->setStyleSheet("QPushButton { background-color: red; }");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_one_clicked()
{
    expression +='1';
    ui->mainlineEdit->setText(expression);
}


void Widget::on_pushButton_two_clicked()
{
    expression +='2';
    ui->mainlineEdit->setText(expression);
}



void Widget::on_pushButton_three_clicked()
{
    expression +='3';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_four_clicked()
{
    expression +='4';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_five_clicked()
{
    expression +='5';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_six_clicked()
{
    expression +='6';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_sevevn_clicked()
{
    expression +='7';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_eight_clicked()
{
    expression +='8';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_nine_clicked()
{
    expression +='9';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_jia_clicked()
{
    expression +='+';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_jian_clicked()
{
    expression +='-';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_cheng_clicked()
{
    expression +='*';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_chu_clicked()
{
    expression +='/';
    ui->mainlineEdit->setText(expression);

}


void Widget::on_pushButton_qingling_clicked()
{
    ui->mainlineEdit->clear();
    expression.clear();
}


void Widget::on_pushButton_huitui_clicked()
{
    expression.chop(1);
    ui->mainlineEdit->setText(expression);
}

// 函数：计算四则运算表达式的值
double evaluateExpression(const QString& expression) {
    // 定义操作符的优先级
    QHash<QString, int> precedence;
    precedence["*"] = 2;
    precedence["/"] = 2;
    precedence["+" ] = 1;
    precedence["-"] = 1;

    // 定义操作符的结合性（左结合或右结合）
    QHash<QString, bool> associativity;
    associativity["*"] = true;
    associativity["/"] = true;
    associativity["+" ] = true;
    associativity["-"] = true;

    // 用于存储后缀表达式的队列
    QStringList postfix;
    // 用于存储操作符的栈
    QStack<QString> operators;

    // 使用正则表达式分割表达式
    QRegularExpression re(R"(\s+|(\d+\.\d+|\d+|[+\-*/()]))");
    QRegularExpressionMatchIterator i = re.globalMatch(expression);

    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        QString token = match.captured(1);

        if (token.isEmpty()) {
            continue;
        }

        if (token.contains(QRegularExpression(R"(\d+\.\d+|\d+)"))) {
            // 如果是数字，直接加入后缀表达式
            postfix.append(token);
        } else if (token == "(") {
            // 如果是左括号，压入操作符栈
            operators.push(token);
        } else if (token == ")") {
            // 如果是右括号，弹出操作符直到遇到左括号
            while (!operators.isEmpty() && operators.top() != "(") {
                postfix.append(operators.pop());
            }
            operators.pop(); // 弹出左括号
        } else {
            // 如果是操作符
            while (!operators.isEmpty() && precedence.contains(operators.top()) &&
                   ((precedence[operators.top()] > precedence[token]) ||
                    (precedence[operators.top()] == precedence[token] && associativity[token]))) {
                postfix.append(operators.pop());
            }
            operators.push(token);
        }
    }

    // 弹出操作符栈中剩余的操作符
    while (!operators.isEmpty()) {
        postfix.append(operators.pop());
    }

    // 计算后缀表达式的值
    QStack<double> values;
    for (const QString& token : postfix) {
        if (token.contains(QRegularExpression(R"(\d+\.\d+|\d+)"))) {
            // 如果是数字，压入值栈
            values.push(token.toDouble());
        } else {
            // 如果是操作符，弹出两个操作数进行计算
            double b = values.pop();
            double a = values.pop();
            if (token == "+") {
                values.push(a + b);
            } else if (token == "-") {
                values.push(a - b);
            } else if (token == "*") {
                values.push(a * b);
            } else if (token == "/") {
                values.push(a / b);
            }
        }
    }

    // 栈顶的值就是最终结果
    return values.pop();
}

void Widget::on_pushButton_equal_clicked()
{
    QString  d=QString::number(evaluateExpression(expression));
    expression.clear();
    ui->mainlineEdit->setText(d);
}

